package chinaums.plugin.redis;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import chinaums.plugin.redis.CacheClient;

/**
 * 
 * @author liyongjie
 * @time 2017年7月19日 下午1:59:46
 * @version v1.0
 * @description 通过实现CacheClient接口实现了父类的方法来进行想过的redis调用操作
 */
@Service
public class JedisClientImpl implements CacheClient {
	/*
	 * Long result = null; try { result = jedis.hset(hkey, key, value); } catch
	 * (Exception e) { jedisPool.returnBrokenResource(jedis); } finally {
	 * jedisPool.returnResource(jedis); } return result;
	 */
	/*
	 * redis一键对多值的方法操作
	 */
	@Autowired
	private JedisPool jedisPool;

	public long setList(String key, String value) {
		Jedis jedis = jedisPool.getResource();
		Long list = null;
		try {
			// List<String> list = jedis.lrange("w_bcard",0,-1);
			list = jedis.lpush(key, value);
		} catch (Exception e) {
			jedisPool.returnBrokenResource(jedis);
		} finally {
			jedisPool.returnResource(jedis);
		}

		return list;
	}
	/*
	 * 获取一键多值的值(non-Javadoc)
	 * @see chinaums.plugin.redis.CacheClient#getList(java.lang.String)
	 */
	@Override
	public List<String> getList(String key) {
		Jedis jedis = jedisPool.getResource();
		List<String> value = null;
		try {
			value = jedis.lrange(key, 0, -1);
		} catch (Exception e) {
			jedisPool.returnBrokenResource(jedis);
		} finally {
			jedisPool.returnResource(jedis);
		}
		return value;
	}
	
	/*
	 * @see chinaums.plugin.redis.CacheClient#get(java.lang.String) 通过键获取到值
	 */
	public String get(String key) {
		String value = null;
		Jedis jedis = jedisPool.getResource();
		try {
			value = jedis.get(key);
		} catch (Exception e) {
			jedisPool.returnBrokenResource(jedis);
		} finally {
			jedisPool.returnResource(jedis);
		}
		return value;
	}

	/*
	 * @see chinaums.plugin.redis.CacheClient#get(byte[])
	 * 一次性通过数组获取多个值(non-Javadoc)
	 */
	public byte[] get(byte[] key) {
		// 定义数组
		byte[] value = null;
		Jedis jedis = jedisPool.getResource();
		try {
			// 实现数组接收
			value = jedis.get(key);
		} catch (Exception e) {
			jedisPool.returnBrokenResource(jedis);
		} finally {
			jedisPool.returnResource(jedis);
		}
		return value;
	}

	/*
	 * 从redis设置单键单值
	 * 
	 * @see chinaums.plugin.redis.CacheClient#set(java.lang.String,
	 * java.lang.String)
	 */
	public String set(String key, String value) {
		Jedis jedis = jedisPool.getResource();
		try {
			value = jedis.set(key, value);
		} catch (Exception e) {
			jedisPool.returnBrokenResource(jedis);
		} finally {
			jedisPool.returnResource(jedis);
		}
		return value;
	}

	/*
	 * 设置一键一值与期满时间条件
	 * 
	 * @see chinaums.plugin.redis.CacheClient#set(java.lang.String,
	 * java.lang.String, int)
	 */
	public String set(String key, String value, int expire) {
		Jedis jedis = jedisPool.getResource();
		try {
			value = jedis.set(key, value);

			if (expire != 0) {
				jedis.expire(key, expire);
			}
		} catch (Exception e) {
			jedisPool.returnBrokenResource(jedis);
		} finally {
			jedisPool.returnResource(jedis);
		}
		return value;
	}

	/*
	 * 通过数组的方式存入多键多值
	 * 
	 * @see chinaums.plugin.redis.CacheClient#set(byte[], byte[])
	 */
	public String set(byte[] key, byte[] value) {
		// 获取时长
		Jedis jedis = jedisPool.getResource();
		String v = null;
		try {
			v = jedis.set(key, value);
		} catch (Exception e) {
			jedisPool.returnBrokenResource(jedis);
		} finally {
			jedisPool.returnResource(jedis);
		}
		return v;
	}

	/*
	 * 通过数组的方式存入多键多值与期满时间条件
	 * 
	 * @see chinaums.plugin.redis.CacheClient#set(byte[], byte[], int)
	 */
	public String set(byte[] key, byte[] value, int expire) {
		// getResource()获取时长
		Jedis jedis = jedisPool.getResource();
		String v = null;
		try {
			v = jedis.set(key, value);
			if (expire != 0) {
				jedis.expire(key, expire);
			}
		} catch (Exception e) {
			jedisPool.returnBrokenResource(jedis);
		} finally {
			jedisPool.returnResource(jedis);
		}
		return v;
	}

	/*
	 * 通过hashset来进行存储
	 * 
	 * @see chinaums.plugin.redis.CacheClient#hget(java.lang.String,
	 * java.lang.String)
	 */
	public String hget(String hkey, String key) {
		String value = null;
		Jedis jedis = jedisPool.getResource();
		try {
			value = jedis.hget(hkey, key);
		} catch (Exception e) {
			jedisPool.returnBrokenResource(jedis);
		} finally {
			jedisPool.returnResource(jedis);
		}
		return value;
	}

	public long hset(String hkey, String key, String value) {
		Jedis jedis = jedisPool.getResource();
		Long result = null;
		try {
			result = jedis.hset(hkey, key, value);
		} catch (Exception e) {
			jedisPool.returnBrokenResource(jedis);
		} finally {
			jedisPool.returnResource(jedis);
		}
		return result;
	}

	/*
	 * 将key存储的数值加一
	 * 
	 * @see chinaums.plugin.redis.CacheClient#incr(java.lang.String)
	 */
	public long incr(String key) {
		Jedis jedis = jedisPool.getResource();
		Long result = null;
		try {
			result = jedis.incr(key);
		} catch (Exception e) {
			jedisPool.returnBrokenResource(jedis);
		} finally {
			jedisPool.returnResource(jedis);
		}
		return result;
	}

	/*
	 * 设置指定过期时间
	 * 
	 * @see chinaums.plugin.redis.CacheClient#expire(java.lang.String, int)
	 */
	public long expire(String key, int second) {
		Jedis jedis = jedisPool.getResource();
		Long result = null;
		try {
			result = jedis.expire(key, second);
		} catch (Exception e) {
			jedisPool.returnBrokenResource(jedis);
		} finally {
			jedisPool.returnResource(jedis);
		}
		return result;
	}

	/*
	 * 以秒为单位返回 key 的剩余过期时间
	 * 
	 * @see chinaums.plugin.redis.CacheClient#ttl(java.lang.String)
	 */
	public long ttl(String key) {
		Jedis jedis = jedisPool.getResource();
		Long result = null;
		try {
			result = jedis.ttl(key);
		} catch (Exception e) {
			jedisPool.returnBrokenResource(jedis);
		} finally {
			jedisPool.returnResource(jedis);
		}
		return result;
	}

	/*
	 * 通过key进行删除value
	 * 
	 * @see chinaums.plugin.redis.CacheClient#del(java.lang.String)
	 */
	public long del(String key) {
		Jedis jedis = jedisPool.getResource();
		Long result = null;
		try {
			result = jedis.del(key);
		} catch (Exception e) {
			jedisPool.returnBrokenResource(jedis);
		} finally {
			jedisPool.returnResource(jedis);
		}
		return result;
	}

	/*
	 * 通过数组形式的key删除value
	 * 
	 * @see chinaums.plugin.redis.CacheClient#del(byte[])
	 */
	public long del(byte[] key) {
		Jedis jedis = jedisPool.getResource();
		Long result = null;
		try {
			result = jedis.del(key);
		} catch (Exception e) {
			jedisPool.returnBrokenResource(jedis);
		} finally {
			jedisPool.returnResource(jedis);
		}
		return result;
	}

	/*
	 * 从存储在键散列删除指定的字段，如果没有这个哈希中存在指定的字段将被忽略，如果键不存在,它将被视为一个空的哈希与此命令将返回0。(non-Javadoc
	 * )
	 * 
	 * @see chinaums.plugin.redis.CacheClient#hdel(java.lang.String,
	 * java.lang.String)
	 */
	public long hdel(String hkey, String key) {
		Jedis jedis = jedisPool.getResource();
		Long result = null;
		try {
			result = jedis.hdel(hkey, key);
		} catch (Exception e) {
			jedisPool.returnBrokenResource(jedis);
		} finally {
			jedisPool.returnResource(jedis);
		}
		return result;
	}

	/*
	 * 通过set的方式进行存储
	 * 
	 * @see chinaums.plugin.redis.CacheClient#keys(java.lang.String)
	 */
	public Set<byte[]> keys(String pattern) {
		Set<byte[]> keys = null;
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
		} catch (Exception e) {
			return null;
		}
		try {
			keys = jedis.keys(pattern.getBytes());
		} catch (Exception e) {
			jedisPool.returnBrokenResource(jedis);
		} finally {
			jedisPool.returnResource(jedis);
		}
		return keys;
	}

	/*
	 * 清空所有(non-Javadoc)
	 * 
	 * @see chinaums.plugin.redis.CacheClient#flushDB()
	 */
	public void flushDB() {
		Jedis jedis = jedisPool.getResource();
		try {
			jedis.flushDB();
		} catch (Exception e) {
			jedisPool.returnBrokenResource(jedis);
		} finally {
			jedisPool.returnResource(jedis);
		}
	}

	/*
	 * 查看所有键的数量(non-Javadoc)
	 * 
	 * @see chinaums.plugin.redis.CacheClient#dbSize()
	 */
	public Long dbSize() {
		Long dbSize = 0L;
		Jedis jedis = jedisPool.getResource();
		try {
			dbSize = jedis.dbSize();
		} catch (Exception e) {
			jedisPool.returnBrokenResource(jedis);
		} finally {
			jedisPool.returnResource(jedis);
		}
		return dbSize;
	}


}
